/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Driver Module                                  */
/*                                                                           */
/*    Copyright (C) 2011 Laszlo Arvai                                        */
/*                                                                           */
/*    ------------------------------------------------------------------     */
/*    HMC5883 3 axis megnetometer sensor driver                              */
/*****************************************************************************/

#ifndef __drvHMC5883_h
#define __drvHMC5883_h

///////////////////////////////////////////////////////////////////////////////
// Includes
#include <krnlTypes.h>

///////////////////////////////////////////////////////////////////////////////
// Constants

// I2C address for drvHMC5883 - base address for drvMCP3424
#define drvHMC5883_BASE_ADDRESS 0x1e

// register addresses
#define drvHMC5883_CONFIG_REG_A         0x00
#define drvHMC5883_CONFIG_REG_B         0x01
#define drvHMC5883_MODE_REG             0x02
#define drvHMC5883_DATAOUT_XMSB_REG     0x03
#define drvHMC5883_DATAOUT_XLSB_REG     0x04
#define drvHMC5883_DATAOUT_ZMSB_REG     0x05
#define drvHMC5883_DATAOUT_ZLSB_REG     0x06
#define drvHMC5883_DATAOUT_YMSB_REG     0x07
#define drvHMC5883_DATAOUT_YLSB_REG     0x08
#define drvHMC5883_DATAOUT_STATUS_REG   0x09
#define drvHMC5883_DATAOUT_IDA_REG      0x0A
#define drvHMC5883_DATAOUT_IDB_REG      0x0B
#define drvHMC5883_DATAOUT_IDC_REG      0x0C

// CONFIG_REG_A bits

// Output Data Rate
#define drvHMC5883_ODR_MASK		0x1c
#define drvHMC5883_ODR_0_75		0x00
#define drvHMC5883_ODR_1_5    0x04
#define drvHMC5883_ODR_3      0x08
#define drvHMC5883_ODR_7_5    0x0C
#define drvHMC5883_ODR_15     0x10
#define drvHMC5883_ODR_30     0x14
#define drvHMC5883_ODR_75     0x18
 
// Measure configuration
#define drvHMC5883_MEASCONF_MASK				0x03
#define drvHMC5883_MEASCONF_NORMAL      0x00
#define drvHMC5883_MEASCONF_BIAS_POS    0x01
#define drvHMC5883_MEASCONF_BIAS_NEG    0x02

// CONFIG_REG_B bits
// Gain settings
#define drvHMC5883_GAIN_MASK		0xe0
#define drvHMC5883_GAIN_0_9    0x00
#define drvHMC5883_GAIN_1_2     0x20
#define drvHMC5883_GAIN_1_9     0x40
#define drvHMC5883_GAIN_2_5     0x60
#define drvHMC5883_GAIN_4_0     0x80
#define drvHMC5883_GAIN_4_6     0xA0
#define drvHMC5883_GAIN_5_5     0xC0
#define drvHMC5883_GAIN_7_9     0xE0

// MODE_REG bits

// Modes
#define drvHMC5883_MODE_MASK				0x03
#define drvHMC5883_MODE_CONTINUOUS  0x00
#define drvHMC5883_MODE_SINGLE      0x01
#define drvHMC5883_MODE_IDLE        0x02
#define drvHMC5883_MODE_SLEEP       0x03

// STATUS_REG bits
#define drvHMC5883_STATUS_RDY				0x01
#define drvHMC5883_STATUS_LOCK			0x02
#define drvHMC5883_STATUS_REN				0x04

// Sensitivity Conversion Values */
#define drvHMC5883_Sensitivity_0_88Ga         1370    // LSB/Ga
#define drvHMC5883_Sensitivity_1_3Ga          1090    // LSB/Ga
#define drvHMC5883_Sensitivity_1_9Ga          820     // LSB/Ga
#define drvHMC5883_Sensitivity_2_5Ga          660     // LSB/Ga
#define drvHMC5883_Sensitivity_4_0Ga          440     // LSB/Ga
#define drvHMC5883_Sensitivity_4_7Ga          390     // LSB/Ga
#define drvHMC5883_Sensitivity_5_6Ga          330     // LSB/Ga
#define drvHMC5883_Sensitivity_8_1Ga          230     // LSB/Ga  --> NOT RECOMMENDED

///////////////////////////////////////////////////////////////////////////////
// Function prototypes
void drvHMC5883Init(void);
dosBool drvHMC5883SetConfiguration(dosByte in_config_a, dosByte in_config_b);
dosBool drvHMC5883SetMode(dosByte in_mode);
dosBool drvHMC5883ReadValues(dosWord* out_x, dosWord* out_y, dosWord* out_z);
dosDWord drvHMC5883ReadID(void);

#endif
